package Graph.AdList;


import Graph.AdMatrix.IGraph;
import Graph.AdMatrix.VNode;
import Graph.AdMatrix.GraphKind;

import java.util.Scanner;

public class ALGraph implements IGraph {
    private GraphKind Kind;
    private int VexNum , arcNum;
    private VNode[] vexs;
    private int v;
    private int w;

    public ALGraph(){
        this(null,0,0,null);
    }
    public ALGraph(GraphKind Kind,int vexNum,int arcNum,VNode[] vexs){
        this.Kind = Kind;
        this.VexNum = vexNum;
        this.arcNum = arcNum;
        this.vexs = vexs;
    }
    public void createGraph(){
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入图的类型；");
        GraphKind Kind = GraphKind.valueOf(sc.next());
        switch (Kind){
            case DG:
                createDG();
                return;
            case UDG:
                createUDG();
                return;
            case DN:
                createDN();
                return;
            case UDN:
                createUDN();
                return;

        }
    }

    @Override
    public int getVexNum() {
        return VexNum;
    }

    @Override
    public int getArcNum() {
        return arcNum;
    }

    @Override
    public Object getVex(int v) throws Exception {
        return null;
    }


    @Override
    public int locateVex(Object vex) {
        return 0;
    }

    @Override
    public int firstAdjVex(int v) {
        return 0;
    }

    private void createUDN() {
        Scanner sc =new Scanner(System.in);
        System.out.println("请分别输入图的顶点数、图的边数");
        VexNum = sc.nextInt();
        arcNum = sc.nextInt();
        vexs = new VNode[VexNum];
        System.out.println("请分别输入图的各个顶点：");
        for(int v=0; v<VexNum; v++)
            vexs[v]=new VNode(sc.next());
        System.out.println("请输入各个边的顶点及其权值：");
        for(int k=0;k<arcNum;k++){
            int v =locateVex(sc.next());
            int u = locateVex(sc.next());
            int value = sc.nextInt();
            addArc(v,u,value);
        }

    }





    private void addArc(int v, int u, int value) {
        ArcNode arc = new ArcNode(u,value);
        arc.nextArc = vexs[v].firstArc;
        vexs[v].firstArc = arc;

    }

    public int nextAdjVex(int v,int w) throws Exception{
        this.v = v;
        this.w = w;
        if(v<0 && v> VexNum)
            throw new Exception("第"+v+"个顶点不存在！");
        VNode vex = vexs[v];
        ArcNode arcvw = null;
        for (ArcNode arc = vex.firstArc; arc!=null;arc =arc.nextArc)
            if (arc.adjVex == w){
                arcvw = arc;
                break;
            }
        if(arcvw!= null && arcvw.nextArc!=null)
            return arcvw.nextArc.adjVex;
        else
            return -1;

    }



    private void createDN() {
        Scanner sc =new Scanner(System.in);
        System.out.println("请分别输入图的顶点数、图的边数");
        VexNum = sc.nextInt();
        arcNum = sc.nextInt();
        vexs = new VNode[VexNum];
        System.out.println("请分别输入图的各个顶点：");
        for(int v=0;v<VexNum;v++)
            vexs[v]=new VNode(sc.next());
        System.out.println("请输入各个边的顶点及其权值：");
        for(int k=0;k<arcNum;k++){
            int v =locateVex(sc.next());
            int u = locateVex(sc.next());
            int value = sc.nextInt();
            addArc(v,u,value);
        }

    }

    private void createUDG() {
    }

    private void createDG() {
    }




}
